package com.ordobill.webapp.action;

import java.util.ArrayList;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.commons.logging.*;
import com.ibatis.sqlmap.client.*;
import com.ordobill.webapp.beans.Report;
import com.ordobill.webapp.common.SqlMapClientManager;
import org.apache.struts.actions.*;

public class ReportAction extends DispatchAction{
    
    private Log log = LogFactory.getFactory().getInstance(this.getClass().getName());
    private SqlMapClient sqlMap = SqlMapClientManager.getSqlMapClient();
    

    /**
     * report 초기 parameter 를 조합
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked") 
    public ActionForward list(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception {
        
        Report reportForm = (Report)form; // 초기 시물레이터 값
        Report reportValueForm = (Report)form; // report 를 위한 셋팅 값
        HttpSession session = request.getSession();
        reportForm.setProId((String)session.getAttribute("unitIDInSession"));
        
      if(reportForm.getTimeOfSimulation() == null){
    	  // run 미 실행시 report parameter
    	  reportValueForm.setCvsData("cvsdata="+reportForm.getProIterNum()+"^"+reportForm.getProStartTime()+"^"+reportForm.getProLifeTime()+"^"+reportForm.getProTd());
      	  reportValueForm.setCvsData2("cvsdata2=");
      }else{
    	  
/*   	plex 화면의 값과 일치하기 위해 계산식 변경
          int _idx =0;
    	  float avgTimeOfSimulationInt = 0;
    	  float avgTimeOfSimulationInt2 = 0;
          for(float a : reportForm.getTimeOfSimulation()){
        	  avgTimeOfSimulationInt += a;
        	  avgTimeOfSimulationInt2 += a;
        	  _idx++;
          }
          avgTimeOfSimulationInt = (avgTimeOfSimulationInt/(float)_idx);
*/
    	  float avgTimeOfSimulationInt = 0;
    	  for(float a : reportForm.getTimeOfSimulation()){
        	  avgTimeOfSimulationInt = a;
          }

    	  
    	  
          int _idx1 = 0;
          float repairTimeInt = 0; 
          for(float a : reportForm.getRepairTime()){
        	  repairTimeInt += a;
        	  _idx1++;
          }
          repairTimeInt = (repairTimeInt/(float)_idx1);


        
        int _idx2 = 0;
        float availabilityTimeInt = 0;
        for( float a : reportForm.getAvailabilityValue()){
        	availabilityTimeInt += a;
        	_idx2++;
        }
        availabilityTimeInt = (availabilityTimeInt/(float)_idx2);
        
        
        String cvsdata2 = "";
        int _idx3 = 0;
    	for( float a : reportForm.getAvailabilityValue()){
    		cvsdata2 += reportForm.getAvailabilityValue()[_idx3]+"^"+(_idx3+1)+"///";
    	  _idx3 ++;
    	}

    	
    	reportValueForm.setCvsData("cvsdata="+reportForm.getProIterNum()+"^"+reportForm.getProStartTime()+"^"+reportForm.getProLifeTime()+"^"+reportForm.getProTd()+"^"+avgTimeOfSimulationInt+"^"+repairTimeInt+"^"+availabilityTimeInt);
    	reportValueForm.setCvsData2("cvsdata2="+cvsdata2);
    	
        request.setAttribute("reportForm", reportForm); 
        request.setAttribute("reportValueForm", reportValueForm);
      }
        return mapping.findForward("list");
    }

}
